﻿// 项目：	Guan.CMS.Domain - Guan.CMS
// 文件名：	CanChangePassword.cs
// 
// 修改时间：2013-03-08 22:01
// 修改人：	Cooclsee

using System.Collections.Generic;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Core.Common;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.Account;
using Guan.CMS.Model.Entities;

namespace Guan.CMS.Domain.Handlers.Account
{
    public class CanChangePassword : IValidationHandler<ChangePasswordCommand>
    {
        private readonly IUserRepository _userRepository;
        public CanChangePassword(IUserRepository userRepository)
        {
            this._userRepository = userRepository;
        }
        public IEnumerable<ValidationResult> Validate(ChangePasswordCommand command)
        {
            User user = this._userRepository.GetById(command.UserId);
            var encoded = Md5Encrypt.Md5EncryptPassword(command.OldPassword);

            if (!user.PasswordHash.Equals(encoded))
            {
                yield return new ValidationResult("OldPassword", "密码错误");
            }
        }
    }
}